package org.study.module.mysql;

import org.study.shell.ShellBase;
import org.study.shell.ShellFile;
import org.study.shell.ShellUser;

import java.util.ArrayList;
import java.util.List;

public class MysqlShell {
    /**
     * mysql前置查看
     */
    public static List<String> preCheck() {
        List<String> commands = new ArrayList<String>();
        commands.add("# 校验安装，应显示 libaio 版本");
        commands.add("rpm -qa | grep mariadb");
        commands.add("# 校验安装，应显示 libaio 版本");
        commands.add("rpm -qa | grep libaio ");
        commands.add("# 校验安装，应显示 numactl 版本");
        commands.add("rpm -qa | grep numactl ");
        return commands;
    }

    /**
     * 前置安装
     *
     * @param softPath 软件上传地址
     */
    public static List<String> preInstall(String softPath) {
        //#-z 是判断为空， -n判断不为空， "$"是防止变量未定义报错
        List<String> commands = new ArrayList<String>();
        commands.add("cd " + softPath);
        commands.add("# 校验安装，应显示 libaio 版本");
        commands.add("mariadb=`rpm -qa | grep mariadb`");
        commands.add("if [ -z \"$mariadb\" ]; then");
        commands.add("    echo \"mariadb已经卸载！\"");
        commands.add("else");
        commands.add("    rpm -e  $mariadb --nodeps");
        commands.add("echo \"卸载mariadb,成功！\"");
        commands.add("fi");
        commands.add("#安装 libaio");
        commands.add("libaio=`rpm -qa | grep libaio`");
        commands.add("if [ -z \"$libaio\" ]; then");
        commands.add("   rpm -ivh libaio-*.rpm --nodeps --force   # 忽略依赖校验");
        commands.add("fi");
        commands.add("numactl=`rpm -qa | grep numactl`");
        commands.add("if [ -z \"$numactl\" ]; then");
        commands.add("   rpm -ivh numactl-*.rpm --nodeps --force   # 忽略依赖校验");
        commands.add("fi");
        return commands;
    }


    public static List<String> changePassword(String password){
        List<String> commands = new ArrayList<String>();
        commands.add("echo \" 关闭服务\"");
        commands.add("sudo service mysqld stop");
        commands.add(ShellFile.replaceContent("s/#skip-grant-tables/skip-grant-tables/g", "/etc/my.cnf"));
        commands.add("echo \" 启动服务\"");
        commands.add("sudo service mysqld start");
        commands.add("mysql -u root <<EOF");
        commands.add("use mysql;");
        commands.add("FLUSH PRIVILEGES;");
        commands.add("ALTER USER 'root'@'localhost' IDENTIFIED BY '" + password + "';");
        commands.add("FLUSH PRIVILEGES;");
        commands.add("EOF");

        commands.add("echo \" 关闭服务\"");
        commands.add("sudo service mysqld stop");
        commands.add(ShellFile.replaceContent("s/skip-grant-tables/#skip-grant-tables/g", "/etc/my.cnf"));
        commands.add("echo \" 启动服务\"");


        commands.add("mysql -u root -p" + password + " <<EOF");
        commands.add("use mysql;");
        commands.add("FLUSH PRIVILEGES;");
        commands.add("CRATE USER 'root'@'%' IDENTIFIED  WITH mysql_native_password BY '" + password + "';");
        commands.add("GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;");
        commands.add("FLUSH PRIVILEGES;");
        commands.add("EOF");
        commands.add("echo \" Mysql允许远程登录\"");
        return commands;
    }

    /**
     * msyql：安装
     *
     * @param softPath    软件上传地址
     * @param installPath 安装地址
     * @return
     */
    public static List<String> install(String softPath, String installPath, String password) {
        List<String> commands = new ArrayList<String>();
        String name = "mysql";
        installPath =  installPath+"/mysql";
        String dataPath = installPath + "/data";
        commands.addAll(ShellUser.addGroup(name));
        commands.addAll(ShellUser.addUser(name, name, null));
        commands.addAll(ShellFile.createPath(installPath));
        commands.addAll(ShellFile.createPath(dataPath));
        commands.add("#查看mysql的安装包");
        commands.addAll(ShellFile.findByName(softPath, "mysql-8.0", "mysql_tar"));
        commands.add("tar -xvf $mysql_tar -C " + installPath);
        commands.add(ShellFile.chownPath(name, name, installPath));
        commands.add("sudo chmod -R 775 " + installPath);
        commands.add("cd " + installPath);
        commands.add("#查询安装后的目录");
        commands.addAll(ShellFile.findByName(installPath, "mysql-8.0", "install_path"));
        commands.add("sudo $install_path/bin/mysqld --initialize --user=" + name + " --basedir=$install_path --datadir=" + dataPath);
        commands.add("sudo cat > /etc/my.cnf << EOF");
        commands.add("[mysqld]");
        commands.add("basedir=$install_path");
        commands.add("datadir=" + dataPath);
        commands.add("port = 3306");
        commands.add("socket=/tmp/mysql.sock");
        commands.add("bind-address=0.0.0.0");
        commands.add("log-error=" + installPath + "/mysqld.log");
        commands.add("pid-file=" + installPath + "/mysqld.pid");
        commands.add("character-set-server=utf8mb4");
        commands.add("collation-server=utf8mb4_unicode_ci");
        commands.add("#跳过密码认证");
        commands.add("skip-grant-tables");
        commands.add("[client]");
        commands.add("default-character-set=utf8mb4");
        commands.add("EOF");
        commands.add("echo \"查看/ect/my.cnf配置\"");
        commands.add("cat /etc/my.cnf");
        List<String> pros = new ArrayList<>();
        pros.add("export MYSQL_HOME=$install_path");
        pros.add("export PATH=\\$PATH:\\$MYSQL_HOME/bin");
        commands.addAll(ShellBase.addProfile(pros, "mysql_env.sh"));
        commands.add("#系统默认查找路径/usr/bin下无mysql命令链接");
        commands.addAll(ShellFile.lnExistDelete("/usr/bin/mysql", "$install_path/bin/mysql"));
        commands.add("#添加到自启动");
        commands.add("sudo cp $install_path/support-files/mysql.server /etc/init.d/mysqld");
        //因文件中存在多个basedir=，所有需要制定行替换
        commands.add(ShellFile.replaceContent("46s|basedir=|basedir=$install_path|","/etc/init.d/mysqld"));
        commands.add(ShellFile.replaceContent("47s|datadir=|datadir=" + dataPath + "|g", "/etc/init.d/mysqld"));
        commands.add("# 添加服务");
        commands.add("sudo chkconfig --add mysqld ");
        commands.add("echo \" 开机自启\"");
        commands.add("sudo chkconfig mysqld on");
        commands.add("echo \" 启动服务\"");
        commands.add("sudo service mysqld start ");
        commands.add("echo \"验证服务状态\"");
        commands.add("sudo service mysqld status ");
        commands.add("mysql -u root <<EOF");
        commands.add("use mysql;");
        commands.add("FLUSH PRIVILEGES;");
        commands.add("ALTER USER 'root'@'localhost' IDENTIFIED BY '" + password + "';");
        commands.add("FLUSH PRIVILEGES;");
        commands.add("EOF");
        commands.add("echo \"root密码是" + password + "\"");
        commands.add("echo \" 关闭服务\"");
        commands.add("sudo service mysqld stop");
        commands.add("echo \" 免密功能关闭\"");
        commands.add(ShellFile.replaceContent("s/skip-grant-tables/#skip-grant-tables/g", "/etc/my.cnf"));
        commands.add("echo \" 启动服务\"");
        commands.add("sudo service mysqld start ");
        commands.add("mysql -u root -p" + password + "<<EOF");
        commands.add("use mysql;");
        commands.add("FLUSH PRIVILEGES;");
        commands.add("CREATE USER 'root'@'%' IDENTIFIED  WITH mysql_native_password BY '" + password + "';");
        commands.add("GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;");
        commands.add("FLUSH PRIVILEGES;");
        commands.add("EOF");
        commands.add("echo \" Mysql允许远程登录\"");
        commands.addAll(ShellBase.firewallAddPort(3306));
        return commands;
    }
}
